package model.connectible;

import model.File;
import model.connections.Connection;
import model.connections.Route;

import java.util.ArrayList;
import java.util.List;

public class Peer implements Connectible {

	private Connection ispConnection;
	private File wantedFile;
    private List<File> storage;
	private Route route;
	private int id;
	
	private int timeOn;
	private int timeOff;
	private boolean on;
	private int timer;
	
	private Peer clone;
	
	public Peer() {
		
	}
	
	public Peer(int id, int timeOn, int timeOff) {
		setTimeOn(timeOn);
		setTimeOff(timeOff);
		setTimer(0);
		setOn(true);
		setId(id);
        storage = new ArrayList<>();
	}

	public Connection getIspConnection() {
		return ispConnection;
	}

	public void setIspConnection(Connection ispConnection) {
		this.ispConnection = ispConnection;
	}
	
	public File getWantedFile() {
		return wantedFile;
	}

	public void setWantedFile(File wantedFile) {
		this.wantedFile = wantedFile;
	}

	public Route getRoute() {
		return route;
	}

	public void setRoute(Route route) {
		this.route = route;
	}

	@Override
	public void connect(Connection connection) {
		setIspConnection(connection);
	}
	
	public String toString() {
		String s = "Peer id: "+id+"\n";
		if (wantedFile != null) {
			s = s+" "+wantedFile.toString();
		} else {
			s = s+" brak pliku";
		}
		s = s+"\n\n";
		if (storage.isEmpty()) {
			s = s+" brak pobranych plikow";
		}
		for (File file : storage) {
			s = s+file.toString()+"\n";
		}
		return s+"\n";
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public int getTimeOn() {
		return timeOn;
	}

	public void setTimeOn(int timeOn) {
		this.timeOn = timeOn;
	}

	public int getTimeOff() {
		return timeOff;
	}

	public void setTimeOff(int timeOff) {
		this.timeOff = timeOff;
	}

	public boolean isOn() {
		return on;
	}

	public void setOn(boolean on) {
		this.on = on;
	}

	public int getTimer() {
		return timer;
	}

	public void setTimer(int timer) {
		this.timer = timer;
	}
	
	public void pushTimer() {
		int threshold;
		if(isOn()) {
			threshold = getTimeOn();
		} else {
			threshold = getTimeOff();
		}
		
		if(getTimer()>=threshold) {
			setTimer(0);
			setOn(!isOn());
		} else {
			setTimer(getTimer()+1);
		}
	}

    public void addFileToStorage(File file) {
        storage.add(file);
    }

    public boolean isSeeedingFile(File file) {
    	for (File f : storage) {
			if (file.equals(f)) {
				return true;
			}
		}
        return false;
    }
    
//    public Peer clone() {
//    	Peer peer = new Peer();
//    	peer.setId(id);
//    	peer.setIspConnection(ispConnection);
//    	peer.setRoute(route);
//    	peer.setTimeOff(timeOff);
//    	peer.setTimeOn(timeOn);
//    	peer.setWantedFile(wantedFile);
//    	peer.setOn(on);
//    	return peer;
//    }
    public Peer getClone() {
    	if (clone == null) {
	    	clone = new Peer(id, timeOn, timeOff);
	    	clone.setWantedFile(wantedFile.clone());
	    	clone.setOn(on);
	    	for (File file : storage) {
				clone.addFileToStorage(file.clone());
			}
    	}
    	return clone;
    }
}
